const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');

const SystemLog = sequelize.define('SystemLog', {
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true
  },
  type: {
    type: DataTypes.ENUM('operation', 'system', 'security', 'error'),
    allowNull: false,
    comment: '日志类型'
  },
  level: {
    type: DataTypes.ENUM('info', 'warning', 'error', 'critical'),
    defaultValue: 'info',
    comment: '日志级别'
  },
  module: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '日志模块'
  },
  action: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '日志动作'
  },
  user_id: {
    type: DataTypes.UUID,
    comment: '关联用户ID'
  },
  ip_address: {
    type: DataTypes.STRING,
    comment: 'IP地址'
  },
  user_agent: {
    type: DataTypes.STRING,
    comment: '用户代理'
  },
  description: {
    type: DataTypes.TEXT,
    comment: '日志描述'
  },
  metadata: {
    type: DataTypes.JSON,
    defaultValue: {},
    comment: '元数据'
  },
  status: {
    type: DataTypes.ENUM('success', 'failure'),
    defaultValue: 'success',
    comment: '日志状态'
  }
}, {
  underscored: true,
  indexes: [
    {
      fields: ['user_id'],
      comment: '关联用户ID索引'
    },
    {
      fields: ['type'],
      comment: '日志类型索引'
    },
    { 
      fields: ['level'],
      comment: '日志级别索引'
    },
    {
      fields: ['module'],
      comment: '日志模块索引'
    },
    {
      fields: ['action'],
      comment: '日志动作索引'
    }
  ],
  comment: '系统日志信息表'
});

module.exports = SystemLog; 